package com.geccocrawler.gecco.demo.fang;

import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.DeriveSchedulerContext;
import com.geccocrawler.gecco.utils.ConnectDb;
import org.apache.commons.dbutils.QueryRunner;
import org.jsoup.helper.StringUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by 抽离 on 2017/5/24 0024.
 */
@PipelineName("fangNextPipeline")
public class FangPipeline implements Pipeline<Fang>{
    Connection connection = ConnectDb.Connect();
    @Override
    public void process(Fang bean) {
        HttpRequest request = bean.getRequest();
        List<String> section = bean.getSection();
        List<String> sellCount = bean.getSellCount();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "insert into fang values(null, ?, ?, ?)";
        for(int i = 0; i < section.size(); i++){
            try {
                queryRunner.update(connection, sql, bean.getRegion(), section.get(i), sellCount.get(i));
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        String nextUrl = bean.getNextUrl();
        if(!StringUtil.isBlank(nextUrl)){
            DeriveSchedulerContext.into(request.subRequest(nextUrl));
        }
    }
}
